package com.gang.controller;

import com.alibaba.fastjson.JSON;
import com.gang.pojo.CommonResult;
import com.gang.pojo.User;
import com.gang.service.UserService;
import com.gang.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "用户服务")
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping ("/login/{username}/{password}")
    @ApiOperation("登录服务")
    public CommonResult login(@PathVariable("username")String username, @PathVariable("password")String password, HttpServletResponse response){
        User user = userService.queryUserByUsername(username);
        if(user!=null&&BCrypt.checkpw(password,user.getPassword())){
            Map<String,Object> info = new HashMap<String,Object>();
            info.put("role","USER");
            info.put("success","SUCCESS");
            info.put("username",username);
            //生成令牌
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(info),null);
            Cookie authorization = new Cookie("Authorization", jwt);
            response.addCookie(authorization);
            return new CommonResult(200,"登录成功",user);
        }
        else {
            return new CommonResult(400,"登录失败",null);
        }
    }

    @ResponseBody
    @RequestMapping ("/register")
    @ApiOperation("注册服务")
    public CommonResult register(@RequestBody User user){
        int i = userService.insertUser(user);
        if(i==0)
        {
            return new CommonResult(400,"注册失败",null);
        }
        else {
            return new CommonResult(200,"注册成功",null);
        }
    }

    @ResponseBody
    @RequestMapping ("/get/{username}")
    @ApiOperation("查询用户")
    public CommonResult getUser(@PathVariable("username")String username){
        User user = userService.queryUserByUsername(username);
        if(user!=null)
        return new CommonResult(200,"查询成功",user);
        else
            return new CommonResult(400,"查询失败",null);
    }

    @ResponseBody
    @RequestMapping ("/getall")
    @ApiOperation("查询所有用户")
    public CommonResult getAllUser(){
        List<User> userList = userService.queryAllUser();
        if(userList!=null)
            return new CommonResult(200,"查询成功",userList);
        else
            return new CommonResult(400,"查询失败",null);
    }

    @ResponseBody
    @RequestMapping ("/getdid/{username}")
    @ApiOperation("查询用户did")
    public CommonResult getUserDid(@PathVariable("username")String username){
        String s = userService.querryUserDid(username);
        return new CommonResult(200,"查询成功",s);
    }

    @ResponseBody
    @PostMapping ("/adddid/{did}/{username}")
    @ApiOperation("插入用户did")
    public CommonResult addUserDid(@PathVariable("did")String did,@PathVariable("username")String username){
        int i = userService.addDid(did, username);
        if(i!=-1)
        return new CommonResult(200,"插入成功",i);
        else
            return new CommonResult(400,"插入失败",i);
    }

    @ResponseBody
    @PostMapping ("/addaddress/{username}/{address}")
    @ApiOperation("插入地址")
    public CommonResult addaddress(@PathVariable("username") String username,@PathVariable("address")String address){
        int i = userService.addaddress(username, address);
        if(i!=-1)
            return new CommonResult(200,"插入成功",i);
        else
            return new CommonResult(400,"插入失败",i);
    }
}

